<?php

namespace app\common\model\service;

use app\common\model\BaseModel;
use common\Common;

//公共函数

/**
 * 上传压缩图片
 * @name LibUploadCompressImage.php
 * @package hfztcy
 * @category model
 * @link http://www.chanekeji.com
 * @author rxy
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2022-06-17
 */
class LibUploadCompressImage extends BaseModel
{
    public $fileObj;
    public $rootPath;
    public $saveDir;
    private $useOriginalName = true;
    private $rename = false;

    public function __construct($dir,$name = 'file')
    {
        $this->fileObj = request()->file($name);
        $this->rootPath = ROOT_PATH . 'public/';
        $this->saveDir = $dir;
        parent::__construct();
    }

    /**
     * Info: 使用原文件名称
     * Date: 2023/2/13 10:49
     */
    public function setUserOriginalName ()
    {
        $this->useOriginalName = false;
        return $this;
    }

    /**
     * Info: 重新设置新名称
     * Date: 2023/2/13 10:50
     */
    public function setRename ()
    {
        $this->rename = true;
        return $this;
    }

    /**
     * Info: 上传资源
     * Date: 2023/2/13 10:50
     */
    public function uploadImage()
    {
        $ROOT = $this->rootPath;
        $save_path = 'upload/' . $this->saveDir . '/';
        $upload_path = str_replace('\\', '/', $ROOT . $save_path);
        if (!$this->fileObj) {
            return Common::errorArray(2, '没有上传！', false);
        }
        $info = $this->fileObj->move($upload_path,$this->useOriginalName);  //保存原图
        if ($info) {
            $filename = $info->getSaveName();
            $date_path = str_replace('\\', '/', rtrim($ROOT . $save_path, '/')) . '/' . date('Ymd');
            if (!file_exists($date_path)) {
                mkdir($date_path, 0777, true);
            }
            if (!$this->rename) {//不改名称
                $url = str_replace('\\', '/', $save_path . $filename);
                $image = \think\Image::open(str_replace('\/', '/', $ROOT . $url));
                $thumb_path = $date_path . '/' . $info->getFilename();
                $path_parts = pathinfo($thumb_path);
                $path = $path_parts['dirname'] . '/' . $path_parts['filename'] . '_thumb.' . $path_parts['extension'];  //缩略图文件名
                $file_path = substr($path, strripos($path, "public") + 6);
                $image->thumb(2000, 1550)->save($path);
                $path = ltrim($file_path, '/');
            } else {
                $oldname = str_replace('\\', '/', rtrim($ROOT . $save_path, '/')) . '/' . $filename;
                $path_parts = pathinfo($oldname);
                $newname = $date_path . '/' . $path_parts['filename'] . '_thumb.' . $path_parts['extension'];  //缩略图文件名
                $file_path = substr($newname, strripos($newname, "public") + 6);
                rename($oldname,$newname);
                $path = ltrim($file_path, '/');
            }
            return Common::errorArray(0, '上传成功！', $path);
        } else {
            return Common::errorArray(1, '上传失败！', false);
        }
    }

    /**
     * Info: 旋转图片
     * Date: 2023/2/13 10:50
     */
    public function rotateImage ($path)
    {
        ini_set("memory_limit","1024M");
        $exif = @exif_read_data($path);
        if (isset($exif['Orientation'])) {
            if ($exif['Orientation'] == 3) {
                $path = LibImageRotate::imageRotate($path,180);
            } elseif ($exif['Orientation'] == 6) {
                $path = LibImageRotate::imageRotate($path,-90);
            } elseif ($exif['Orientation'] == 8) {
                $path = LibImageRotate::imageRotate($path,90);
            }
        }
        return $path;
    }
}

?>